package com.example.MyDessin;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.LinkedList;

/**
 * Created by richard on 31/01/14.
 */
   class Cercle {
        int xc, yc, rayon;
        private Paint paint;
        public Cercle(int x, int y, int r) {
            xc= x; yc = y; rayon = r;
            paint = new Paint();
            //paint.setColor(Color.BLUE);
            paint.setColor(Color.rgb((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256))) ;
        }
        public void draw(Canvas canvas) {
                canvas.drawCircle(xc, yc, rayon, paint);
         }
}
public class Dessin extends View  implements View.OnTouchListener {

    LinkedList<Cercle> cercles;
    public Dessin(Context context) {
        super(context);

    }
    public Dessin(Context context, AttributeSet attrs) {
        super(context, attrs);
        cercles = new LinkedList<Cercle>();
        this.setOnTouchListener(this);
    }
    @Override
    public void onDraw(Canvas canvas) {

            for (Cercle cercle: cercles)
                cercle.draw(canvas);
        // ADD to get View size
        Paint paint = new Paint();
        //canvas.drawPaint(paint);
        int viewWidth = this.getWidth();
        int viewHeight = this.getHeight();

        paint.setColor(Color.WHITE);
        paint.setTextSize(32);
        canvas.drawText(viewWidth + " -- " + viewHeight, 20, 40, paint);


    }
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                cercles.add(new Cercle(x, y, 40));
               // Log.e("MESSAGE \n\n\n", x + "  " + y + " --" + cercles.size());
                break;// toucher en x,y
             case MotionEvent.ACTION_MOVE:
                    Cercle cercle = cercles.getLast();
                    cercle.rayon=(int)Math.sqrt((x-cercle.xc)*(x-cercle.xc) + (y-cercle.yc)*(y-cercle.yc));
                break;

             //mouvementversx,y
               }
        this.invalidate();

        return true;
    }


}
